package com.yuandian.management.utils;


import com.yuandian.management.vo.Weekday;

import java.sql.Date;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 工具类
 * <p>
 *
 * @author lijunhang
 * @date 2021/12/13
 * @since 1.0.0
 */

public class Utils {

    //获取某一年的所有日期及每个日期对应的是星期几,1和7为周日和周六
    public static List<Weekday> getYearOfDays(String startTime, String endTime) throws ParseException {
        List<Weekday> list=new ArrayList<>();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");

        Calendar beginDate=new GregorianCalendar();
        Calendar endDate=new GregorianCalendar();
        DateFormatSymbols dfs=new DateFormatSymbols(Locale.CHINA);
        String[] weeks=dfs.getWeekdays();

        beginDate.setTime(format.parse(startTime));
        endDate.setTime(format.parse(endTime));

        //为了包含结束日期
//        endDate.add(Calendar.DAY_OF_YEAR,1);

        while (beginDate.before(endDate)){
            Weekday weekDay=new Weekday();
//            System.out.println("日期："+new Date(beginDate.getTime().getTime())+","+weeks[beginDate.get(Calendar.DAY_OF_WEEK)]+","+beginDate.get(Calendar.DAY_OF_WEEK));
            weekDay.setDay(format.format(new Date(beginDate.getTime().getTime())));
            weekDay.setDayWeek(beginDate.get(Calendar.DAY_OF_WEEK));
            weekDay.setDayWeekStr(weeks[beginDate.get(Calendar.DAY_OF_WEEK)]);

            if (beginDate.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY ||beginDate.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY){
                weekDay.setType(2);
            }else{
                weekDay.setType(1);
            }

            beginDate.add(Calendar.DAY_OF_YEAR,1);
            list.add(weekDay);
        }
        return list;
    }
}
